package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.AnnouncementType;
import com.cloudfun.campusshare.common.model.vo.AnnouncementAddVO;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.service.AnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Set;

/**
 * @author : zhangyongjie
 * @version : 1.0
 * @createTime : 2020/6/5 18:17
 * @description :
 */

@RestController
@RequestMapping("/v1/announcement")
@Api(value = "提现")
@Validated
@Slf4j
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;

    @ApiOperation(value = "创建公告", notes = "创建公告")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @AuthRequired
    BaseResult create(@ApiParam(value = "AnnouncementAddVO", required = true) @Valid @RequestBody AnnouncementAddVO announcementAddVO) {
        announcementService.createAnnouncement(announcementAddVO);
        return BaseResultFactory.produceSuccess();
    }

    @ApiOperation(value = "校验公告", notes = "校验公告")
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    @AuthRequired
    BaseResult check(@ApiParam(value = "公告", required = true) @RequestBody AnnouncementAddVO announcementAddVO) {
        return announcementService.check(announcementAddVO);
    }

    @ApiOperation(value = "公告列表", notes = "公告列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @AuthRequired
    BaseResult queryList(Pageable pageable,
                         @ApiParam(value = "关键字", required = false) @RequestParam(required = false) String keyword,
                         @ApiParam(value = "公告类型", required = false) @RequestParam(required = false) Set<AnnouncementType> type,
                         @ApiParam(value = "开始时间", required = false) @RequestParam(required = false) String startTime,
                         @ApiParam(value = "结束时间", required = false) @RequestParam(required = false) String endTime) {
        return BaseResultFactory.produceSuccess(announcementService.queryAnnouncementList(pageable, keyword, type, startTime, endTime));
    }

    @ApiOperation(value = "下线公告", notes = "下线公告")
    @RequestMapping(value = "/offline/{announcementId}", method = RequestMethod.POST)
    @AuthRequired
    BaseResult offLine(@PathVariable String announcementId) {
        announcementService.offlineAnnouncement(announcementId);
        return BaseResultFactory.produceSuccess();
    }


    @ApiOperation(value = "删除公告", notes = "删除公告")
    @RequestMapping(value = "/delete/{announcementId}", method = RequestMethod.POST)
    @AuthRequired
    BaseResult delete(@PathVariable String announcementId) {
        announcementService.deleteAnnouncement(announcementId);
        return BaseResultFactory.produceSuccess();
    }


    @ApiOperation(value = "用户端公告列表", notes = "用户端公告列表")
    @RequestMapping(value = "/listForUser", method = RequestMethod.GET)
    BaseResult listForUser(@ApiParam(value = "公告类型") @RequestParam AnnouncementType type,
                           @ApiParam(value = "洗衣点id") @RequestParam(required = false) String washPointId ){
        return BaseResultFactory.produceSuccess(announcementService.queryForUserPartList(type,washPointId));
    }

}

